(function() {"use strict";var __module = CC_EDITOR ? module : {exports:{}};var __filename = 'preview-scripts/assets/resources/Script/Ssg/Utils/ProtoBufUtil.js';var __require = CC_EDITOR ? function (request) {return cc.require(request, require);} : function (request) {return cc.require(request, __filename);};function __define (exports, require, module) {"use strict";
cc._RF.push(module, '2f00coVn7BB6rGhiZhRcS5E', 'ProtoBufUtil', __filename);
// Script/Ssg/Utils/ProtoBufUtil.js

"use strict";

//导入protobufjs模块
var ProtoBuf = require("protobufjs");
var Base64 = require('js-base64').Base64;

var ProtoBufUtil = cc.Class({
    extends: cc.Component,

    properties: {},

    statics: {
        protoEncode: function protoEncode(protoUrl, mesName, uploadData, messageBuffer) {
            var self = this;
            cc.loader.loadRes(protoUrl, function (err, msg) {
                cc.log("start load file");
                if (err) {
                    cc.error(err.message || err);
                    return;
                }
                //console.log("protoUtil:",ProtoBuf)
                var Build = ProtoBuf.loadProto(msg);
                if (Build) {
                    var MsgMoudle = Build.build(mesName);
                    if (MsgMoudle) {
                        var msgMoudle = new MsgMoudle();
                        for (var variable in msgMoudle) {
                            if (uploadData[variable] != undefined) {
                                msgMoudle[variable] = uploadData[variable];
                            }
                        }
                        var array = msgMoudle.toBuffer();
                        messageBuffer(array);
                    } else {
                        cc.log("MsgMoudle is null");
                    }
                } else {
                    cc.log("Build Faild!");
                }
            });
        },

        protoDecode: function protoDecode(protoName, mesName, array, resultMeg) {
            var self = this;
            cc.loader.loadRes(protoName, function (err, msg) {
                cc.log("start load file");
                if (err) {
                    cc.error(err.message || err);
                    return;
                }
                // cc.log('------message:' + msg);

                var Build = ProtoBuf.loadProto(msg);

                if (Build) {

                    var MsgMoudle = Build.build(mesName);
                    if (MsgMoudle) {
                        // cc.log("MsgMoudle is not null");
                        var message = MsgMoudle.decode(array);
                        resultMeg(message);
                        return message;
                    } else {
                        cc.log("MsgMoudle is null");
                    }
                } else {
                    cc.log("Build Faild!");
                }
            });
        },

        str2bytes: function str2bytes(str) {
            var bytes = [];
            for (var i = 0, len = str.length; i < len; ++i) {
                var c = str.charCodeAt(i);
                var byte = c & 0xff;
                bytes.push(byte);
            }
            return bytes;
        },

        byteToString: function byteToString(arr) {
            // cc.log('----------array:' + arr.length);
            if (typeof arr === 'string') {
                return arr;
            }
            var str = '',
                _arr = arr;
            for (var i = 0; i < _arr.length; i++) {
                // cc.log('----------array:');
                var one = _arr[i].toString(2),
                    v = one.match(/^1+?(?=0)/);
                if (v && one.length == 8) {
                    var bytesLength = v[0].length;
                    var store = _arr[i].toString(2).slice(7 - bytesLength);
                    for (var st = 1; st < bytesLength; st++) {
                        store += _arr[st + i].toString(2).slice(2);
                    }
                    str += String.fromCharCode(parseInt(store, 2));
                    i += bytesLength - 1;
                } else {
                    // cc.log('----------string:' + str);
                    str += String.fromCharCode(_arr[i]);
                }
            }
            return str;
        }

    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    start: function start() {}
}

// update (dt) {},
);

module.exports = ProtoBufUtil;

cc._RF.pop();
        }
        if (CC_EDITOR) {
            __define(__module.exports, __require, __module);
        }
        else {
            cc.registerModuleFunc(__filename, function () {
                __define(__module.exports, __require, __module);
            });
        }
        })();
        //# sourceMappingURL=ProtoBufUtil.js.map
        